import {delay,call,put,all,takeEvery,takeLatest,take} from 'redux-saga/effects';
// 导入actions
import {get_message,get_message_async} from './actions';
import {GET_MESSAGE_ASYNC} from './action-types';


// root saga
function* rootSaga(){
    // 通过all触发其他saga异步处理函数
    yield all([watchSaga(),getList()]);
}  

// watcher saga
function* watchSaga(){
    yield takeEvery(GET_MESSAGE_ASYNC,getMessage);   
}

// worker saga
function* getMessage(){
    // 延时1s,模拟异步请求
    yield delay(1000);
    // 准备模拟数据
    const data=[{
        id:1,
        content:'众里寻他千百度,蓦然回首,那人却在灯火阑珊处',
        author:'辛弃疾',
        createTime:new Date()
    }];
    // 通过put触发对应的action
    yield put(get_message(data));
}
// 请求远程数据
function* getList(){
    const request=yield call(fetch,'https://cnodejs.org/api/v1/topics');
    const data=yield request.json();
    console.log(data);
}

export default rootSaga;